<?php

/*%setcsname%*/
$name = 'employees';
/*END*/

// uncomment to use page directly instead of through pocket tempate
//include_once '../../boot.php';
include_once $name . '_fields.php';
include_once $name . '_class.php';


$actionurl = $name . '_proc.php';

/*%fieldspec%*/
$fieldspec = &$employees_spec;
/*END*/


$flt = new CF_FilterQueryBase($fieldspec);

$Filters = $flt->get_filter_vals();

$action = $_REQUEST['action'];

// set values from filters here, so they will be overridden

foreach($fieldspec as $column=>$colspec){
	if(!empty($colspec['key'])){
		$keyslist[] = $column; 
	}
}
	
if($action == 'edit'){

	$req = new CF_Request;
	$keyset = $req->GetReqParamsByList($keyslist);

	//  if key is complete, then we proceed
	if(is_array($keyset)){

	$keys = $keyset[0];
	
		global $dbh;
		/*%setquery%*/
		$query = 'SELECT * FROM employees WHERE 1 ';
		/*END*/
		foreach($keys as $column=>$value){
			$query .= ' AND ' . $column . ' = ' . ':' . $column . ' ';
		}
		$stmt = $dbh->prepare($query);
		foreach($keys as $column=>$value){
			$stmt->bindParam(':' . $column, $value);
		}
	
		$stmt->execute();

	    /*** fetch the results ***/
	    $result = $stmt->fetchAll();
	    $result_row = $result[0];

	    
	}

}else{
	$action = 'add';

	$result_row = $flt->GetEqSet();
}
	
/*%Lookup%*/
	$employeeLookups = new employeesLookupList();
	$employeeLookups->SetFilter($Filters);
	$employeeLookups->ResolveSpecLists($fieldspec);
/*END*/	

$ar_SelToCtl['T'] = 'CtlText';
$ar_SelToCtl['D'] = 'CtlSelect';
$ar_SelToCtl['CAL'] = 'CtlJQDate';
$ar_SelToCtl['C'] = 'CtlCheckbox';



// Create control array for each control

	foreach($fieldspec as $column=>$colspec){
		if(!empty($colspec['show'])){
			if(empty($colspec['select'])){
				
			}else{
				$ctls[$column]['name'] = $column;
				$ctls[$column]['id'] = $column;
				
				$label = $column;
	
			}
		}
	}
	
?>
<html>
<head>

<head>
<body>

<form name='autofrm_<?php print $name ?>' method='get' action='<?php print $actionurl ?>'>


<?php	

foreach($ctls as $column=>$spec){
	print MakePhpCtlWithLabel($column,$ar_SelToCtl[$fieldspec[$column]['select']],$spec, $result_row[$column],$fieldspec[$column]['list']);
}


?>



<input type='submit' value='<?php print $action ?>' name='mode' />
</form>

<hr />
<form name='autofrm_<?php print $name ?>' method='get' action='<?php print $actionurl ?>'>
<!--%ctls%-->

<!--END-->
</form>

</body>
</html>
